
#define _LED_C_


#include "HBS1621.h"


//数字显码
//uchar const __ROM dis_code[]={0xfa,0x30,0xbc,0xf4,0x66,0xd6,0xd0,0x70,0x00,0xf6 }; //0 1 2 3 4 5 6 7 8 9

#define BIAS 0X52 //1/3偏压，4背极
#define RC256K 0X30           //系统时钟选择为片内RC振荡器
#define WDTDIS1 0X0A           //禁止WDT溢出标志输出
#define TIMERDIS 0X08           //时基输出禁止
#define SYSEN 0X02              //打开系统时钟
#define SYSDIS 0X00             //关闭系统时钟和偏压发生器
#define LCDON 0X06            //打开LCD偏压发生器
#define LCDOFF 0X04        //关闭LCD偏压发生器
#define TONEON  0X12       //打开音频输出
#define TONEOFF 0X10       //关闭音频输出


u8 f_Ht1621_Writing = 0; //HT1621正在写标志

void init_HBS1621()
{
	
}
//*****************************************************************************************
//函数名：void HBS1621D_init(void)
//输入：无
//输出：无
//功能描述：初始化th1621
//*****************************************************************************************
void HBS1621D_init(void)
{

	if (HT1621_USE == 1)
	{
		GPIO_InitTypeDef GPIO_InitStruct;

	    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);

	    GPIO_StructInit(&GPIO_InitStruct);
	    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_1 | GPIO_Pin_3 |GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
	    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	    GPIO_Init(GPIOA, &GPIO_InitStruct);

	    HBS1621_EN_0;

		SendCmd(RC256K);	//系统时钟选择为片内RC振荡器
		SendCmd(SYSEN);		//打开系统时钟
		SendCmd(WDTDIS1);	 //禁止WDT溢出标志输出
		SendCmd(TIMERDIS);	//时基输出禁止
		SendCmd(BIAS);		//1/3偏压，4背极


		SendCmd(LCDON);		//打开LCD偏压发生器


	//	ClearAll_1621(0xff);

		ClearAll_1621(0x00);//清除残留数据
		HBS1621D_Display();

	}


}


static void Delay_1us(void)
{
	u8 nCount = 4;
	while (nCount != 0)
	{
		nCount--;
	}
}

void Start_HT1621(void)                      //初始化HT1621
{


	HBS1621_DAT_1;
	HBS1621_WR_1;
	HBS1621_CS_1;

	Delay_1us();
	Delay_1us();
	Delay_1us();

}

//*****************************************************************************************
//函数名：void SendHightBit_1621(void)
//输入：无
//输出：无
//功能描述：发高位
//*****************************************************************************************
void SendHightBit_1621(u8 dat , u8 cnt)
{
	u8 i;
	for (i = 0; i < cnt; i ++)
	{
		if ((dat & 0x80) == 0)   HBS1621_DAT_0;
		else HBS1621_DAT_1;
		Delay_1us();
		HBS1621_WR_0;
		Delay_1us();

		HBS1621_WR_1;
		dat = dat << 1;
	}
}

//*****************************************************************************************
//函数名：void SendLowBit_1621(void)
//输入：无
//输出：无
//功能描述：发低位
//*****************************************************************************************
void SendLowBit_1621(u8 dat, u8 cnt)
{
	u8 i;
	for (i = 0; i < cnt; i ++)
	{
		if ((dat & 0x01) == 0)  HBS1621_DAT_0;
		else HBS1621_DAT_1;
		HBS1621_WR_0;
		Delay_1us();
		Delay_1us();		//不够改4US
		HBS1621_WR_1;
		dat = dat >> 1;
	}
}
//*****************************************************************************************
//函数名：void SendCmd(void)
//输入：无
//输出：无
//功能描述：发控制命令
//*****************************************************************************************
void SendCmd(u8 command)
{
	Start_HT1621();
	HBS1621_CS_0;
	Delay_1us();
	Delay_1us();
	SendHightBit_1621(0x80, 4);		//发送命令，ID=100+0
	SendHightBit_1621(command, 8);
	HBS1621_DAT_0;
	HBS1621_CS_1;
}
//*****************************************************************************************
//函数名：void Write_1621(void)
//输入：无
//输出：无
//功能描述：写入低4
//*****************************************************************************************
u8 addr_chande = 0, dat_change = 0; //供引脚改变用，为了画PCB方便
u8 dat_change0, dat_change1, dat_change2, dat_change3;

void Write_1621(u8 addr, u8 dat)
{

	f_Ht1621_Writing = 1; //正在写数据

	/*
	dat_change0=(dat&0x01)<<3;
	dat_change1=(dat&0x02)<<1;
	dat_change2=(dat&0x04)>>1;
	dat_change3=(dat&0x08)>>3;

		dat_change=((dat_change3)|(dat_change2)|(dat_change1)|(dat_change0));

		*/
	dat_change = dat & 0x0f;



	switch (addr)
	{
	case 0:
		addr_chande = HT1621_ADDR0;
		break;
	case 1:
		addr_chande = HT1621_ADDR1;
		break;
	case 2:
		addr_chande = HT1621_ADDR2;
		break;
	case 3:
		addr_chande = HT1621_ADDR3;
		break;
	case 4:
		addr_chande = HT1621_ADDR4;
		break;
	case 5:
		addr_chande = HT1621_ADDR5;
		break;
	case 6:
		addr_chande = HT1621_ADDR6;
		break;
	case 7:
		addr_chande = HT1621_ADDR7;
		break;
	case 8:
		addr_chande = HT1621_ADDR8;
		break;
	case 9:
		addr_chande = HT1621_ADDR9;
		break;
	case 10:
		addr_chande = HT1621_ADDR10;
		break;
	case 11:
		addr_chande = HT1621_ADDR11;
		break;
	case 12:
		addr_chande = HT1621_ADDR12;
		break;
	case 13:
		addr_chande = HT1621_ADDR13;
		break;
	case 14:
		addr_chande = HT1621_ADDR14;
		break;
	case 15:
		addr_chande = HT1621_ADDR15;
		break;
	case 16:
		addr_chande = HT1621_ADDR16;
		break;
	case 17:
		addr_chande = HT1621_ADDR17;
		break;
	case 18:
		addr_chande = HT1621_ADDR18;
		break;
	case 19:
		addr_chande = HT1621_ADDR19;
		break;
	case 20:
		addr_chande = HT1621_ADDR20;
		break;
	case 21:
		addr_chande = HT1621_ADDR21;
		break;
	case 22:
		addr_chande = HT1621_ADDR22;
		break;
	case 23:
		addr_chande = HT1621_ADDR23;
		break;
	case 24:
		addr_chande = HT1621_ADDR24;
		break;
	case 25:
		addr_chande = HT1621_ADDR25;
		break;
	case 26:
		addr_chande = HT1621_ADDR26;
		break;
	case 27:
		addr_chande = HT1621_ADDR27;
		break;
	case 28:
		addr_chande = HT1621_ADDR28;
		break;
	case 29:
		addr_chande = HT1621_ADDR29;
		break;
	case 30:
		addr_chande = HT1621_ADDR30;
		break;
	case 31:
		addr_chande = HT1621_ADDR31;
		break;
	default :
		break;






	}







	Start_HT1621();
	HBS1621_CS_0;

	SendHightBit_1621(0xa0, 3);
	SendHightBit_1621(addr_chande << 2, 6);
	SendLowBit_1621(dat_change, 4);
	HBS1621_CS_1;



	f_Ht1621_Writing = 0; //没在写数据

}

//*****************************************************************************************
//函数名：void WriteAll_1621(void)
//输入：无
//输出：无			WriteAll_1621(1,LCD_DIS,8);
//功能描述：连续写 : 写入1621地址   数组或存储地址   几个
//说明：
//对RAM中的数据进行连续地址的相同操作时, 只需在首操作时使用一次命令识别码和
//地址码, 后续操作的命令识别码和地址码均可省略。也就是说,在对显示RAM中的数据进行连续地
//址的相同操作时, 可以不受命令格式规定的数据个数为4的限制。在这种情况下,操
//作地址将自动加1
//*****************************************************************************************

void WriteAll_1621(u8 addr, u8 *p, u8 cnt)
{
	u8 i;
	Start_HT1621();
	addr = addr << 1;		//地址为低六位，往左移1位才从高位开始发
	HBS1621_CS_0;


	SendHightBit_1621(0xa0, 3);
	SendHightBit_1621(addr, 6);
	for (i = 0; i < cnt; i ++, p++)
	{
		SendLowBit_1621(*p, 8);
	}
	HBS1621_CS_1;
}


void ClearAll_1621(u8 udata)
{
	u8 i, addr = 9;
	HBS1621_CS_0;
	SendHightBit_1621(0xa0, 3);
	SendHightBit_1621(addr << 2, 6);
	for (i = 0; i < 7; i++)
	{
		SendLowBit_1621(udata, 8);
	}
	HBS1621_CS_1;
}


void HBS1621D_ENABLE(void)//LED点阵显示使能
{

	HBS1621D_init();//初始化

}

void HBS1621D_DISABLE(void)//LED点阵显示禁止
{

	//防止残影
	ClearAll_1621(0x00);//清除残留数据
//     HBS1621_EN=1;//	关
	HBS1621_DAT_0;//
	HBS1621_WR_0;//
	HBS1621_CS_0;//

}

extern sys_status_t sys_status;
     

typedef union {
      uint8_t value;
      struct {
       	uint8_t bit0: 1; 
	    uint8_t bit1: 1;  
	    uint8_t bit2: 1;  
	    uint8_t bit3: 1;  
	    uint8_t bit4: 1;  
	    uint8_t bit5: 1;  
	    uint8_t bit6: 1;  
	    uint8_t bit7: 1; 
      } bits; 

} dis_data_t;
const uint8_t NUM_TABLE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

dis_data_t uDisplay_Buf[7];

void HBS1621D_Display(void)//显示
{
	u8 i, addr = 9;

	if(sys_status.on_off)
	{
		HBS1621_EN_0;
		HBS1621_CS_0;
		SendHightBit_1621(0xa0, 3);
		SendHightBit_1621(addr << 2, 6);

		// sys_status.neck_battery=25;
		// sys_status.remote_battery=65;
		// sys_status.electric=1;
		// sys_status.electric_grade=5;

		// sys_status.shock=1;
		// sys_status.shock_grade=7;

		// sys_status.voice=1;
		// sys_status.voice_grade=8;

		// sys_status.light=1;
		// sys_status.gps=1;
		// sys_status.lock=1;
		// sys_status.dog1=1;
		// sys_status.dog2=1;
		// sys_status.dog3=1;
		//sys_status.digital_value=9;

		uDisplay_Buf[0].bits.bit3=1;//L6
		uDisplay_Buf[0].bits.bit2=sys_status.neck_battery>80?1:0;//L5
		uDisplay_Buf[0].bits.bit1=sys_status.neck_battery>60?1:0;//L4
		uDisplay_Buf[0].bits.bit0=sys_status.neck_battery>40?1:0;//L3
		uDisplay_Buf[0].bits.bit4=sys_status.neck_battery>20?1:0;//L2
		
		uDisplay_Buf[6].bits.bit4=sys_status.remote_battery>80?1:0;//L10
		uDisplay_Buf[6].bits.bit7=sys_status.remote_battery>60?1:0;//L9
		uDisplay_Buf[6].bits.bit5=sys_status.remote_battery>40?1:0;//L8
		uDisplay_Buf[6].bits.bit6=sys_status.remote_battery>20?1:0;//L7

		uDisplay_Buf[2].bits.bit0=sys_status.electric;//T1
		uDisplay_Buf[0].bits.bit7=sys_status.electric_grade>0?1:0;//S1
		uDisplay_Buf[1].bits.bit3=sys_status.electric_grade>1?1:0;//S2
		uDisplay_Buf[1].bits.bit7=sys_status.electric_grade>2?1:0;//S3
		uDisplay_Buf[2].bits.bit3=sys_status.electric_grade>3?1:0;//S4
		uDisplay_Buf[2].bits.bit7=sys_status.electric_grade>4?1:0;//S5
		uDisplay_Buf[3].bits.bit3=sys_status.electric_grade>5?1:0;//S6
		uDisplay_Buf[3].bits.bit7=sys_status.electric_grade>6?1:0;//S7
		uDisplay_Buf[4].bits.bit3=sys_status.electric_grade>7?1:0;//S8

		uDisplay_Buf[6].bits.bit0=sys_status.shock;//T2
		uDisplay_Buf[0].bits.bit6=sys_status.shock_grade>0?1:0;//S9
		uDisplay_Buf[1].bits.bit2=sys_status.shock_grade>1?1:0;//S10
		uDisplay_Buf[1].bits.bit6=sys_status.shock_grade>2?1:0;//S11
		uDisplay_Buf[2].bits.bit2=sys_status.shock_grade>3?1:0;//S12
		uDisplay_Buf[2].bits.bit6=sys_status.shock_grade>4?1:0;//S13
		uDisplay_Buf[3].bits.bit2=sys_status.shock_grade>5?1:0;//S14
		uDisplay_Buf[3].bits.bit6=sys_status.shock_grade>6?1:0;//S15
		uDisplay_Buf[4].bits.bit2=sys_status.shock_grade>7?1:0;//S16
		
		uDisplay_Buf[4].bits.bit4=sys_status.voice;//T3
		uDisplay_Buf[0].bits.bit5=sys_status.voice_grade>0?1:0;//S17
		uDisplay_Buf[1].bits.bit1=sys_status.voice_grade>1?1:0;//S18
		uDisplay_Buf[1].bits.bit5=sys_status.voice_grade>2?1:0;//S19
		uDisplay_Buf[2].bits.bit1=sys_status.voice_grade>3?1:0;//S20
		uDisplay_Buf[2].bits.bit5=sys_status.voice_grade>4?1:0;//S21
		uDisplay_Buf[3].bits.bit1=sys_status.voice_grade>5?1:0;//S22
		uDisplay_Buf[3].bits.bit5=sys_status.voice_grade>6?1:0;//S23
		uDisplay_Buf[4].bits.bit1=sys_status.voice_grade>7?1:0;//S24

		uDisplay_Buf[1].bits.bit4=sys_status.light;//T4
		uDisplay_Buf[3].bits.bit0=sys_status.gps;//T5
		uDisplay_Buf[5].bits.bit0=sys_status.lock;//T6
		uDisplay_Buf[1].bits.bit0=sys_status.dog1;//T7
		uDisplay_Buf[2].bits.bit4=sys_status.dog2;//T8
		uDisplay_Buf[5].bits.bit4=sys_status.dog3;//T9
		{
			uint8_t temp10=NUM_TABLE[sys_status.digital_value/10];
			uDisplay_Buf[3].bits.bit4=temp10>>0;//1A
			uDisplay_Buf[5].bits.bit5=temp10>>1;//1B
			uDisplay_Buf[5].bits.bit6=temp10>>2;//1C
			uDisplay_Buf[5].bits.bit7=temp10>>3;//1D
			uDisplay_Buf[6].bits.bit3=temp10>>4;//1E
			uDisplay_Buf[6].bits.bit1=temp10>>5;//1F
			uDisplay_Buf[6].bits.bit2=temp10>>6;//1G

			temp10=NUM_TABLE[sys_status.digital_value%10];
			uDisplay_Buf[4].bits.bit0=temp10>>0;//2A
			uDisplay_Buf[4].bits.bit5=temp10>>1;//2B
			uDisplay_Buf[4].bits.bit6=temp10>>2;//2C
			uDisplay_Buf[4].bits.bit7=temp10>>3;//2D
			uDisplay_Buf[5].bits.bit3=temp10>>4;//2E
			uDisplay_Buf[5].bits.bit1=temp10>>5;//2F
			uDisplay_Buf[5].bits.bit2=temp10>>6;//2G

		}
		
		for (i = 0; i < 7; i++)
		{
			SendLowBit_1621(uDisplay_Buf[i].value, 8);
		}
		HBS1621_CS_1;
	}
	else
	{
		ClearAll_1621(0);
		HBS1621_EN_1;
	}
	
}

